<?php
/**
 * Created by PhpStorm.
 * User: heitao
 * Date: 2018/5/13
 * Time: 16:07
 */

namespace manniu\controller;

use think\Config;
use think\Cache as tpCache;
use manniu\model\Redis;

class Cache
{
    /**
     * 获取缓存
     * @param string $key           redis键名/tp5支持的所有缓存名
     * @param string $type          redis专有参数;hash/set/zset
     * @param string|array $field   redis专有参数
     * 1.field为空时表示全部;
     * 2.$type为hash时，field表示hash数据类型的field
     * 3.$type为set时，field表示set数据类型的member【无需填写】
     * 4.$type为sortedset时【只支持zRange方法】，field必须为数组，且其中$field['min']为查询的开始,$field['num']为查询的条数,$field['order']为查询的排序
     * @return mixed
     */
    public static function get($key, $type = 'default', $field = []){
        if(!$key){
            return false;
        }
        $data = false;
        if(self::isRedis()){
            $redis = Redis::instance();
            switch(strtolower(trim($type))){
                case 'hash':
                    $data = $redis->hGet($key, $field);
                    break;
                case 'set':
                    $data = $redis->sMembers($key);
                    break;
                case 'sortedset':
                    $field['min'] = isset($field['min']) && is_integer($field['min']) ? $field['min'] : 0;
                    $field['num'] = isset($field['num']) && is_integer($field['num']) ? $field['num'] : -1;
                    $field['order'] = isset($field['order']) && ($field['order'] == 'desc' || $field['order'] == 'asc') ? $field['order'] : 'desc';
                    $data = $redis->zRange($key, $field['min'], $field['num'], $field['order']);
                    break;
                default:
                    $data = $redis->get($key);
            }
        }else{
            $data = tpCache::get($key);
        }
        tpCache::$readTimes++;
        return $data;
    }

    /**
     * @param string $key           redis键名/tp5支持的所有缓存名
     * @param string|array $value   字符串或一维数组
     * @param null $expire          过期时间
     * @param string $type          redis专有参数;参数类型
     * @return bool
     */
    public static function set($key, $value, $expire = null, $type = 'default'){
        if(!$key){
            return false;
        }
        $result = false;
        if(self::isRedis()){
            $redis = Redis::instance();
            $expire = (int)$expire;
            switch(strtolower(trim($type))){
                case 'hash':
                    $result = $redis->hMset($key, $value, $expire);
                    break;
                case 'set':
                    $result = $redis->sAdd($key, $value, $expire);
                    break;
                case 'sortedset':
                    $result = $redis->zAdd($key, $value, $expire);
                    break;
                default:
                    $result = $redis->set($key, $value, $expire);
            }
        }else{
            $result = tpCache::set($key, $value, $expire);
        }
        tpCache::$writeTimes++;
        return $result;
    }

    public static function rm($key, $type = 'default', $field = ''){
        if(!$key){
            return false;
        }
        $result = false;
        if(self::isRedis()){
            $redis = Redis::instance();
            if(in_array($type, ['hash','set','sortedset','default'])){
                $result = $redis->delete($key, $type, $field);
            }else{
                $result = false;
            }
        }else{
            $result = tpCache::rm($key);
        }
        tpCache::$writeTimes++;
        return $result;
    }

    /**
     * 判断缓存类型是否为redis
     * @return bool
     */
    public static function isRedis(){
        if(strtolower(Config::get('cache.type')) == 'redis'){
            return true;
        }
        return false;
    }
}